#include <stdio.h>

void writeFile() {
    FILE *file = fopen("/Users/xinzhixuan/test.c", "w+");
    fputc('c', file);
    fputc('\n', file);
    fputs("hello world\n", file);
    fprintf(file, "hello %d, hello %.2f \n", 1, 1.0);
    fclose(file);
}
void readFile() {
    FILE *file = fopen("/Users/xinzhixuan/test.c", "r");
    char c = fgetc(file);
    printf("读入的字符：%c \n", c); // 读入的字符：c
    c = fgetc(file);
    printf("读入的字符：%c \n", c); // 读入的字符：是个回车
    char buffer[1024];
    char *str = fgets(buffer, 1024, file);
    printf("读入的一行字符串：%s \n", str); // 读入的一行字符串：hello world
    int d = 0;
    float f = 0.0f;
    fscanf(file, "hello %d, hello %f", &d, &f);
    printf("读入格式化字符串中的内存：d:%d, f:%.2f \n", d,
           f); // 读入格式化字符串中的内存：d:1, f : 1.00 fclose(file);
}

void readFile2() {
    FILE *file = fopen("/Users/xinzhixuan/test.c", "r");
    char buffer[1024];
    while (fgets(buffer, 1024, file) != NULL) {
        printf("读入的一行字符串：%s \n",
               buffer); // 读入的一行字符串：hello world
    };
    fclose(file);
}

int main() {
    FILE *file = fopen("/Users/xinzhixuan/test.c", "w");
    fclose(file);
    writeFile();
    readFile();
    readFile2();
    return 0;
}